package com.alcj.service.impl;

import com.alcj.common.utils.CmnUtil;
import com.alcj.common.utils.DateUtil;
import com.alcj.mapper.UserMapper;
import com.alcj.model.User;
import com.alcj.service.UserService;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.DateUtils;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * @author :chenxh
 * @version :v1.0
 * @Package :com.alcj.service.impl
 * @Description :${TODO}(用一句话描述改文件的作用)
 * @date :2017/9/5 11:23
 */
@Service
public class UserServiceImpl  implements UserService{
    @Autowired
    UserMapper userMapper;

    @Override
    public User getUser() {
        return userMapper.findUserInfo();
    }

    /**
     * @Author: chenxh
     * @Description:用户注册保存
     * @Date: 2017/9/7 16:29
     * @param: record
     */
    @Override
    public int registerUser(User record) throws Exception {
        //  生成唯一标识
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        record.setUserid(uuid);
        // 密码
        record.setUserpwd(CmnUtil.EncodeMD5(record.getUserpwd()));
        // 有效状态
        record.setDelflag("0");
        // 操作人
        record.setOptionid(uuid);
        // 操作时间
        record.setOptiontime(DateUtil.getSysDate());
        // 返回结果
        return this.userMapper.insertSelective(record);
    }

    /**
     * @Author: chenxh
     * @Description:用户登录验证
     * @Date: 2017/9/7 16:30
     * @param: record
     */
    @Override
    public String checkUserInfo(User record, HttpSession session) throws Exception {
        // 返回结果
        User checkUser = this.userMapper.selectByUserName(record.getUsername());
        if(CmnUtil.isEmpty(checkUser)){
            return "用户名或手机号不存在";
        }

        if(!CmnUtil.isEmpty(record.getUserpwd()) && !CmnUtil.EncodeMD5(record.getUserpwd()).equals(checkUser.getUserpwd())){
            return "密码不正确，请重新输入";
        }

        // 记录登录用户
        session.setAttribute("User", checkUser);

        return "";
    }

    /**
     * @Author: chenxh
     * @Description:用户注册验证
     * @Date: 2017/9/7 16:30
     * @param: record
     */
    @Override
    public boolean checkUserExist(String tagName) throws Exception {
        // 返回结果
        User checkUser = this.userMapper.selectByUserName(tagName);

        if(CmnUtil.isEmpty(checkUser)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @Author: chenxh
     * @Description:用户登录验证
     * @Date: 2017/9/7 16:30
     *
     * @param: userid
     * @param: userpwd
     */
    @Override
    public boolean checkPassword(String userId,String userPwd) throws Exception {

        // 返回结果
        User checkUser = this.userMapper.selectByPrimaryKey(userId);

        if(!CmnUtil.isEmpty(checkUser.getUserpwd()) && CmnUtil.EncodeMD5(userPwd).equals(checkUser.getUserpwd())){

            return true;

        }else{

            return false;
        }

    }
    /**
     * @Author: chenxh
     * @Description:获取用户信息
     * @Date: 2017/9/7 16:30
     * @param: record
     */
    @Override
    public User getUserInfo(User record) throws Exception {
        // 返回结果
        User resultUser = this.userMapper.selectByPrimaryKey(record.getUserid());

        return resultUser;
    }

    /**
     * @Author: chenxh
     * @Description:更新用户信息
     * @Date: 2017/9/7 16:31
     * @param: record
     */
    @Override
    public int editUserInfo(User record) throws Exception {

        if (!CmnUtil.isEmpty(record.getUserpwd())){
        // 密码
        record.setUserpwd(CmnUtil.EncodeMD5(record.getUserpwd()));}

        // 返回结果
        return this.userMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * @Author: chenxh
     * @Description:获取所有用户
     * @Date: 2017/9/7 16:31
     * @param: record
     */
    @Override
    public List<User> getAllUser(User record) throws Exception {
        // 返回结果
        List<User> resultUser = this.userMapper.selectAllUser();
        return resultUser;
    }

    /**
     * @Author: chenxh
     * @Description:更新用户信息
     * @Date: 2017/9/7 16:31
     * @param: record
     */
    @Override
    public int uplPwdByTelphone(User record) throws Exception {

        if (!CmnUtil.isEmpty(record.getUserpwd())){
            // 密码
            record.setUserpwd(CmnUtil.EncodeMD5(record.getUserpwd()));
        }

        return this.userMapper.updateByTelphone(record);
    }
}
